from PyQt4.QtGui import *
from PyQt4.QtCore import *

from core import cmds
from core.base import Expression
from ui.models import DataModel, DataHandleItem, AttrItem
from ui.delegates import AttrDelegate


class EditorButtonWidget(QWidget):
    def __init__(self, item, parent):
        QWidget.__init__(self, parent)
        
        layout = QHBoxLayout()
        self.editor = item.get_editor()
        self.button = QToolButton(self)
        
        layout.addWidget(self.editor)
        layout.addWidget(self.button)
        
        self.button_act = QAction(self)
        self.button_act.setText("-")
        self.button.setDefaultAction(self.button_act)
        
        layout.setSpacing(0)
        layout.setMargin(0)
        self.setLayout(layout)

class AttrNameWidget(QLabel):
    def __init__(self, component, attr):
        QLabel.__init__(self)
        self.setText(attr.name)
        self.setContextMenuPolicy(Qt.ActionsContextMenu)
        
        self.component = component
        self.attr = attr
        
        self.tgl_exp_act = QAction(self)
        self.connect(self.tgl_exp_act, SIGNAL("triggered()"), self.toggle_expression)
        
        self.addAction(self.tgl_exp_act)
        
        self.update_actions()
        
    def update_actions(self):
        if self.attr.is_expression(self.component):
            self.tgl_exp_act.setText("Remove Expression")
        else:
            self.tgl_exp_act.setText("Create Expression")
        
    def toggle_expression(self):
        if self.attr.is_expression(self.component):
            setattr(self.component, self.attr.name, self.attr.default_value(self.component))
        else:
            setattr(self.component, self.attr.name, Expression())
        self.update_actions()

class DataEditorWidget(QTreeView):
    def __init__(self, parent=None):
        QTreeView.__init__(self, parent)
        self.setModel(DataModel())
        self.connect(self.model(), SIGNAL("itemChanged (QStandardItem *)"), self.item_changed)
        #self.setItemDelegate(AttrDelegate())
        #self.setSelectionBehavior(QAbstractItemView.SelectItems)
        #self.setEditTriggers(QAbstractItemView.On)
        
    def set_component(self, comp):
        self.model().clear()
        self.model().set_input_data(comp, comp.input_attrs())
        self.model().set_output_data(comp, comp.output_attrs())
        
        self.expand(self.model().indexFromItem(self.model().input_parent))
        self.expand(self.model().indexFromItem(self.model().output_parent))
        self.header().resizeSections(QHeaderView.ResizeToContents)
        
    def set_attr_widget(self, item):
        widget = AttrNameWidget(item.component, item.attr)
        widget.setAutoFillBackground(True)
        index = self.model().indexFromItem(item)
        self.setIndexWidget(index, widget)
        
    def item_changed(self, item):
        if isinstance(item, AttrItem):
            self.set_attr_widget(item)
            return
        
        if not item.isEditable() or not isinstance(item, DataHandleItem):
            return
        data = item.get_data()
        index = self.model().indexFromItem(item)
        if data.has_children():
            widget = EditorButtonWidget(item, self)
            widget.button.setText("+")
            self.connect(widget.button_act, SIGNAL("triggered()"), data.new_child)
        elif data.has_parent():
            widget = EditorButtonWidget(item, self)
            widget.button.setText("-")
            self.connect(widget.button_act, SIGNAL("triggered()"), data.remove)
        else:
            widget = item.get_editor()
        widget.setAutoFillBackground(True)
        self.setIndexWidget(index, widget)
            
            

class DataEditorView(ni.View):
    component = ni.Reference(type=ni.Component)
    widget = DataEditorWidget
    
    def component_changed(self, *args):
        pass
        #print 'UPDAING DATA EDITOR'
        #self.update_widgets()
    
    def update_widget(self, widget):
        print self.__class__.component
        print self, self.__class__, dir(self.__class__)
        print self.component
        #if self.component is None: return
        widget.set_component(self.component)
        
    def current_changed(self, comp):
        
        if not cmds.is_component(comp):
            return
        
        self.component = comp
        self.component.__class__.change.disconnect(self.update_widgets)
        self.component.__class__.change.connect(self.update_widgets)
        self.update_widgets()
        
        